const query = require('../../dbConfig/index')
const { generateToken } = require('../../utils/middleware')
const parentLogin = async (ctx,next)=>{
	const body = ctx.request.body
	console.log(body)
	const parentInfo = await query(`select * from parent where parent_tel=? and parent_password=?`,[body.userName,body.password])
	const teacherInfo = parentInfo.length &&  await query(`select * from teacher where classes IN (${parentInfo[0].classes_id})`)
	const studentInfo = parentInfo.length &&  await query(`select * from student where student_id = '${parentInfo[0].student_id}'`)
	const token = generateToken(parentInfo[0].student_id)
	ctx.body={code:0,data:{parent:parentInfo,token:token,teacher:teacherInfo,student:studentInfo},msg:'成功'}
	next()
}
const getScoreDetail = async (ctx,next)=>{
	const res = await query(`select t.*,a.*,s.* from transcript t left join achievement a ON t.achievement_id = a.id LEFT JOIN speciality s ON s.speciality_id = a.speciality where t.student_id='${ctx.request.body.student_id}'`)
	console.log(res)
	const groupedData = res.reduce((acc, obj) => {
		const key = obj.startTime;
		if (!acc[key]) {
		  acc[key] = { startTime: key, data: [] };
		}
		acc[key].data.push({ student_id: obj.student_id,achievement_id:obj.achievement_id,score:obj.score,paper_name:obj.paper_name,speciality_name:obj.speciality_name }); // 可以根据需要添加更多字段
		return acc;
	  }, {});
	  
	  const result = Object.values(groupedData);
	ctx.body={code:0,data:result,msg:'成功'}
	next()
}
module.exports = {
	parentLogin,
	getScoreDetail
}